
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCartItemDiscounts: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.shopCartItemDiscounts = []
    this.setData({
      shopCartItemDiscounts:this.shopCartItemDiscounts
    })
  },
  onCountMinus: function (e) {
    // var index = e.currentTarget.dataset.index;
    var scindex = e.currentTarget.dataset.scindex;
    var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
    var prodCount = shopCartItemDiscounts[scindex].shopCartItems[0].prodCount;
    if (prodCount > 1) {
      this.updateCount(shopCartItemDiscounts, scindex, -1);
    }
  },
  onCountPlus(e,data) { //增加商品数量
    var scindex = e.currentTarget.dataset.scindex;
    var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
    this.updateCount(shopCartItemDiscounts, scindex, 1); //去除index
  },
  updateCount: function (shopCartItemDiscounts, scindex, prodCount) { //更新商品状态
    this.data.shopCartItemDiscounts[scindex].shopCartItems[0].prodCount += prodCount; 
    //index修改为0
    this.setData({
      shopCartItemDiscounts: shopCartItemDiscounts
    });
    wx.setStorageSync('cart', this.shopCartItemDiscounts)
    this.calTotalPrice();//计算总价
  },
  /**
   * 计算购物车总额
   */
  calTotalPrice: function () {
    var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
    var totalMoney = 0;
    for (var i = 0; i < shopCartItemDiscounts.length; i++) {
      var cItems = shopCartItemDiscounts[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        if (cItems[j].checked) {
          totalMoney += cItems[j].prodCount*cItems[j].price
        }
      }
    }

    this.setData({
      totalMoney: totalMoney
    });
  },
  onSelectedItem: function (e) {
    var index = e.currentTarget.dataset.index;// 获取data- 传进来的index
    var scindex = e.currentTarget.dataset.scindex;

    var shopCartItemDiscounts = this.data.shopCartItemDiscounts;// 获取购物车列表
    var checked = shopCartItemDiscounts[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
    shopCartItemDiscounts[scindex].shopCartItems[index].checked = !checked; // 改变状态

    shopCartItemDiscounts[scindex].shopCartItems[index].isChecked = !shopCartItemDiscounts[scindex].shopCartItems[index].isChecked
    this.setData({
      shopCartItemDiscounts: shopCartItemDiscounts
    });
    this.checkAllSelected();//检查全选状态
    this.calTotalPrice();//计算总价
  },
  /**
   * 检查全选状态
   */
  checkAllSelected: function () {
    var allChecked = true;
    var shopCartItemDiscounts = this.data.shopCartItemDiscounts;

    var flag = false;
    for (var i = 0; i < shopCartItemDiscounts.length; i++) {
      var cItems = shopCartItemDiscounts[i].shopCartItems;
      for (var j = 0; j < cItems.length; j++) {
        if (!cItems[j].checked) {
          allChecked = !allChecked;
          flag = true;
          break;
        }
      }
      if(flag){
        break;
      }
    }
    this.setData({
      allChecked: allChecked
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    // onLoad: function (options) {
    // },
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      this.calTotalPrice()
      
      //接收加入购物车物品信息
      let data = this.data.shopCartItemDiscounts
      if(getApp().globalData.datas!=undefined){ //检测全局变量判断是否为加入购物车
        data.push(getApp().globalData.datas) //若为是则添加进data
      }
      this.setData({
      shopCartItemDiscounts: data
      })
      getApp().globalData.datas = undefined //清空全局变量防止onshow页面反复添加物品

      if(getApp().globalData.isShopped!=undefined){
        this.onDelBasket()
        getApp().globalData.isShopped = undefined
      }
      var that = this
      console.log(that.data.shopCartItemDiscounts.length)
      wx.setTabBarBadge({
        index:2,
        text:""+that.data.shopCartItemDiscounts.length
      })
  },
/*
跳转结算页面并提交订单
*/
  toFirmOrder(){
    let data = this.data.shopCartItemDiscounts
    data = this.checkIsClick(data) //检测商品是否被选中
    wx.navigateTo({
      url: '../order-detail/order-detail',
      success:function(res){
      res.eventChannel.emit("dataPoster",data)  //发布事件 dataPoster
    }
    })
    
  },  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  checkIsClick(data){
    let allowData=[]
    for(let a=0;a<data.length;a++){
      if(data[a].shopCartItems[0].isChecked==true){
        allowData.push(data[a])
      }
    }
    return allowData
  },
  checkIsNotClick(data){
    let allowData=[]
    console.log(data)
    for(let a=0;a<data.length;a++){
      if(data[a].shopCartItems[0].isChecked!=true){
        allowData.push(data[a])
      }
    }
    return allowData
  },
  onDelBasket(){ //清除购物车列表
    let data = this.checkIsNotClick(this.data.shopCartItemDiscounts)
    this.data.shopCartItemDiscounts=data
    this.setData({
      shopCartItemDiscounts:data
    })
  }
})